package org.jtb.quickr;

import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

class ContactsGetListRequest extends SignedMethodRequest {
    private ContactsGetListCriteria getListCriteria;
    private String staticUrl;
    
    protected ContactsGetListRequest(String baseUrl, String apiKey, String secret, AuthResponse.Token authToken, ContactsGetListCriteria getListCriteria, String staticUrl) throws QuickrException {
        super(baseUrl, apiKey, secret, authToken, "flickr.contacts.getList");
        this.getListCriteria = getListCriteria;
        this.staticUrl = staticUrl;
    }
    
    
    protected Map<String,String> getArgs() {        
        if (getListCriteria.getFilter() != null) {
            putArg("filter", getListCriteria.getFilter().toString());
        }
        if (getListCriteria.getPage() != null) {
            putArg("page", getListCriteria.getPage().toString());
        }
        if (getListCriteria.getPerPage() != null) {
            putArg("per_page", getListCriteria.getPerPage().toString());
        }
        
        return super.getArgs();
    }
    
    public ContactsResponse execute() throws QuickrException {
        FlickrUrl furl = new SignedMethodUrl(getBaseUrl(), getArgs(), getApiKey(), getSecret(), getAuthToken(), getMethod());
        
        Get get = new Get(furl.getUrl());
        InputStream is = get.execute();
        
        ContactsResponse cres = new ContactsResponse(is, staticUrl);
        
        return cres;
    }
}
